package com.crm.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.crm.entity.Customer;

public class CustomerDao extends HibernateDaoSupport {

	public void add(Customer customer) {
		this.getHibernateTemplate().save(customer);
	}

	public List<Customer> selectAllCustomer() {
		return (List<Customer>) this.getHibernateTemplate().find("from Customer");
	}

	public Integer selectTotalCount() {
		List<Long> find = (List<Long>) this.getHibernateTemplate().find("select count(*) from Customer");
		if(find!=null&&find.size()!=0){
			Long total = find.get(0);
			int count  = total.intValue();
			return count;
		}
		return null;
		
	}

	public List<Customer> selectCustomerByPage(Integer begin, Integer pageSize) {
		//DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
		//List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
		Criteria createCriteria = this.getSessionFactory().getCurrentSession().createCriteria(Customer.class);
		createCriteria.setFirstResult(begin);
		createCriteria.setMaxResults(pageSize);
		List list = createCriteria.list();
		return list;
	}

	public Customer findByCid(Customer customer) {
		List<Customer> find = (List<Customer>) this.getHibernateTemplate().find("from Customer where cid = ?", customer.getCid());
		if(find!=null&&find.size()!=0){
			return find.get(0);
		}
		return null;
	}

	public void updateCustomer(Customer customer) {
		this.getHibernateTemplate().update(customer);
	}

	public void deleteCustomerByCid(Customer customer) {
		this.getHibernateTemplate().delete(customer);
	}

	public List<Customer> queryByCriteria(Customer customer) {
		DetachedCriteria criteria = DetachedCriteria.forClass(customer.getClass());
		if(customer.getClevel()!=null&&!"".equals(customer.getClevel())){
			criteria.add(Restrictions.eq("clevel", customer.getClevel()));
		}else if(customer.getCname()!=null&&!"".equals(customer.getCname())){
			criteria.add(Restrictions.like("cname", "%"+customer.getCname()+"%"));
		}else if(customer.getCsource()!=null&&!"".equals(customer.getCsource())){
			criteria.add(Restrictions.like("csource", "%"+customer.getCsource()+"%"));
		}
		return (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
	}

	public List queryByLevel(Customer customer) {
		Session currentSession = this.getSessionFactory().getCurrentSession();
		String sql = "select count(*) as sum,c.clevel from t_customer c group by c.clevel";
		SQLQuery createSQLQuery = currentSession.createSQLQuery(sql);
		createSQLQuery.setResultTransformer(Transformers.aliasToBean(HashMap.class));
		List list = createSQLQuery.list();
		return list;
	}

	public List queryBySource(Customer customer) {
		Session currentSession = this.getSessionFactory().getCurrentSession();
		String sql = "select count(*) as sum,c.csource from t_customer c group by c.csource";
		SQLQuery createSQLQuery = currentSession.createSQLQuery(sql);
		createSQLQuery.setResultTransformer(Transformers.aliasToBean(HashMap.class));
		return createSQLQuery.list();
	}
	

	

}
